package com.common.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.common.component.Pagination;
import com.common.manager.FuncInfoManager;
import com.common.manager.SystemInfoManager;
import com.common.model.MenuFuncModel;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/menu/func")
public class FuncInfoController extends BaseController{

	 private static String modelUrl="/management/menu/func/";
	 
    @Autowired
	private FuncInfoManager funcInfoManager;
    
    
    @Autowired
	private SystemInfoManager systemInfoManager;
    

    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("menuInfoModel") MenuFuncModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		List<DataMap> dataList = funcInfoManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("objList", dataList);
		return modelView;
	}
	

	
	@RequestMapping("/{menuId}/funcList")
	public ModelAndView funcList(HttpServletRequest request,HttpServletResponse response,@PathVariable("menuId") String menuId){
		
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		try {
			IData param = new DataMap();
			param.put("menuId", (Integer.valueOf(menuId)));
			List<Object> dataList = funcInfoManager.getObjectList(param);
			modelView.addObject("objList", dataList);
			modelView.addObject("menuId", menuId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelView;
	}
	

	/**
	 * 功能：执行批量列表更新操作
	 * @param menuInfoModel
	 * @return
	 */
	@RequestMapping("saveAll")
	public Object saveAll(HttpServletRequest request,HttpServletResponse response) {
		
		try{
			String[] intIds = request.getParameterValues("intID");
			String[] funcNames= request.getParameterValues("funcName");
			String[] funcCodes = request.getParameterValues("funcCode");
			String[] menuIds = request.getParameterValues("menuId");
			String[] remarks = request.getParameterValues("remark");
			int opNum = 0;
			if(menuIds!=null){
				MenuFuncModel menuInfoModel;
				for (int i = 0; i < menuIds.length; i++) {
					menuInfoModel = new MenuFuncModel();
					if(StringUtils.isNotEmpty(intIds[i])){
						menuInfoModel.setIntID(Integer.valueOf(intIds[i]));
					}
					menuInfoModel.setFuncName(funcNames[i]);
					menuInfoModel.setFuncCode(Integer.valueOf(funcCodes[i]));
					menuInfoModel.setMenuId(menuIds[i]);
					menuInfoModel.setRemark(remarks[i]);
					if(menuInfoModel.getIntID()!=null){
						 opNum =funcInfoManager.updateObject(menuInfoModel);
					}else{
						 opNum =funcInfoManager.insertObjectAndGetID(menuInfoModel);
					}
				}
			}
			if(opNum>0){
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}

	/**
	 * 功能：执行更新操作
	 * @param menuInfoModel
	 * @return
	 */
	@RequestMapping("saveOrUpdate")
	public Object saveOrUpdate(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("menuInfoModel") MenuFuncModel menuInfoModel) {
		
		try{
			int opNum = 0;
			if(menuInfoModel.getIntID()!=null){
				 opNum =funcInfoManager.updateObject(menuInfoModel);
			}else{
				 opNum =funcInfoManager.insertObjectAndGetID(menuInfoModel);
			}
			if(opNum>0){
					return super.jsonReturn(response,200);
				}else{
					return super.jsonReturn(response,300);
				}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行单记录删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/{intID}/delete")
	public Object delete(HttpServletResponse response,@PathVariable("intID") int intID) {
		String message="";
		try{
			IData dataMap = new DataMap();
			dataMap.put("intID", intID);
			int opNum =funcInfoManager.deleteObject(dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
			} catch (Exception e) {
				return super.jsonReturn(response,300,e.getMessage(),false);
			}
	}
	
	
	/**
	 * 功能：执行新单记录删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/deleteNew")
	public Object deleteNew(HttpServletResponse response) {
		try{
			return super.jsonReturn(response,200,false);
			} catch (Exception e) {
				return super.jsonReturn(response,300,e.getMessage(),false);
			}
	}
	
}